Release 10.1A: OpenEdge Getting Started:
Object-oriented Programming
Overriding methods
A class definition that inherits from another class can define a method to override a
PUBLICorPROTECTEDmethod in its class hierarchy as long as it is not markedFINAL. The method must have the same method name, access modifier, return type, and number of parameters, and each corresponding parameter must be of the same mode (INPUT,OUTPUT, orINPUT-OUTPUT) and of the same data type as the method it is overriding. However, aPUBLICsubclass method can override aPROTECTEDsuper class method, if you want. The overriding method must also use theOVERRIDEkeyword to assure the compiler that the override is intended. The method that is overridden does not need to be in the immediate super class. If Progress does not find a matching method in the immediate super class in the hierarchy, it searches further up the hierarchy. If no matching method is found in the hierarchy then Progress returns a compile-time error.Method overriding is the means by which the language supports polymorphism. In some languages it is valid to define multiple methods with the same name but different signatures, a practice known as overloading. Overloading of methods is not supported in this release of OpenEdge. If a method has the same name as another method further up its class hierarchy, but the signatures do not match, Progress returns a compile-time error.
Once a method has been overridden, all invocations of that method call the method in the bottom-most subclass where it is defined. There is no direct access to the super class's version of an overridden method from outside the class. However, an overridden method in a subclass can invoke the same method in its super class using the
SUPERsystem reference along with the method name. For more information, see the "Calling a super class method" section.If the super class's method is defined as
PRIVATE, the method is not inherited by the subclass. If the subclass defines a method of the same name, it is not overriding the super class method because the super class's definition is not available to the subclass. In this case, the subclass method is entirely independent of the super class method. Because of this, it does not matter whether the methods match in their return type, access modifier, or parameters. Also in this case, use of theOVERRIDEkeyword on the method is not appropriate.If the super class's non-private method is defined as
FINAL, no subclass can override that method. The compiler returns an error if you attempt to compile a subclass that has a method with the same name as aFINALmethod in a super class, regardless if the signatures match.The following sample class references and extends the sample subclass,
acme.myObjs.CustObj(see the "Sample classes" section). This sample extension overrides theGetCustomerName( )method to return an E-mail address with the customer name:
This extension adds a new temp-table to provide the E-mail address, which can be initialized by another method, or even the constructor for
acme.myObjs.NECustomer(see the following section). Note that only methods can be overridden, constructors and destructors are implicitlyFINALand cannot be overridden in a subclass.
|
Copyright © 2005 Progress Software Corporation www.progress.com Voice: (781) 280-4000 Fax: (781) 280-4095 |